#include "catrowwidget.h"

CatRowWidget::CatRowWidget(QMap<QString, QStringList> opened_album_attrs_)
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
readSettings();

opened_album_attrs = opened_album_attrs_;
current_record = QSqlRecord();
current_index = QModelIndex();
is_new = false;

QLineEdit * catLE = qobject_cast<QLineEdit *>(catComboBox->lineEdit());
catLE->setMaxLength(128);
catComboBox->addItems( opened_album_attrs.value("catalogs") );
catComboBox->setCurrentIndex(-1);

QLineEdit * currLE = qobject_cast<QLineEdit *>( currComboBox->lineEdit() );
currLE->setMaxLength(32);
currComboBox->addItems( opened_album_attrs.value("currencies") );
currComboBox->setCurrentIndex(-1);

QObject::connect( commitButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( save_clicked() ) );
QObject::connect( cancelButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( close() ) );

}

CatRowWidget::~CatRowWidget()
{

}

void CatRowWidget::set_record( QSqlRecord record,
                               QModelIndex index )
{
    if (!record.isNull("CAT_NAME") )
    {
    QString s = record.value("CAT_NAME").toString();
    catNameCheckBox->setChecked(true);
    catComboBox->setEditText(s);
    }
    else
    {
    catNameCheckBox->setChecked(false);
    catComboBox->clearEditText();
    }

    if (!record.isNull("INT_ID") )
    {
    QString s = record.value("INT_ID").toString();
    idCheckBox->setChecked(true);
    idLineEdit->setText(s);
    }
    else
    {
    idCheckBox->setChecked(false);
    idLineEdit->clear();
    }

    if (!record.isNull("CAT_NOTE") )
    {
    QString s = record.value("CAT_NOTE").toString();
    noteCheckBox->setChecked(true);
    noteLineEdit->setText(s);
    }
    else
    {
    noteCheckBox->setChecked(false);
    noteLineEdit->clear();
    }

    if (!record.isNull("CAT_PR_F") )
    {
    double i = record.value("CAT_PR_F").toDouble();
    priceFCheckBox->setChecked(true);
    prFDoubleSpinBox->setValue(i);
    }
    else
    {
    priceFCheckBox->setChecked(false);
    }

    if (!record.isNull("CAT_PR_VF") )
    {
    double i = record.value("CAT_PR_VF").toDouble();
    priceVFCheckBox->setChecked(true);
    prVFDoubleSpinBox->setValue(i);
    }
    else
    {
    priceVFCheckBox->setChecked(false);
    }

    if (!record.isNull("CAT_PR_XF") )
    {
    double i = record.value("CAT_PR_XF").toDouble();
    priceXFCheckBox->setChecked(true);
    prXFDoubleSpinBox->setValue(i);
    }
    else
    {
    priceXFCheckBox->setChecked(false);
    }

    if (!record.isNull("CAT_PR_UNC") )
    {
    double i = record.value("CAT_PR_UNC").toDouble();
    priceUNCCheckBox->setChecked(true);
    prUNCDoubleSpinBox->setValue(i);
    }
    else
    {
    priceUNCCheckBox->setChecked(false);
    }

    if (!record.isNull("CAT_CURR") )
    {
    QString s = record.value("CAT_CURR").toString();
    currCheckBox->setChecked(true);
    currComboBox->setEditText(s);
    }
    else
    {
    currCheckBox->setChecked(false);
    currComboBox->clearEditText();
    }
current_record = QSqlRecord(record);
current_index = QModelIndex(index);
}

QSqlRecord CatRowWidget::read_record(QSqlRecord record)
{
    if (catNameCheckBox->isChecked() )
    {
    record.setValue( "CAT_NAME", QVariant( catComboBox->currentText() ) );
    }
    else
    {
    record.setNull("CAT_NAME");
    }

    if (idCheckBox->isChecked() )
    {
    record.setValue( "INT_ID", QVariant( idLineEdit->text() ) );
    }
    else
    {
    record.setNull("INT_ID");
    }

    if (noteCheckBox->isChecked() )
    {
    record.setValue( "CAT_NOTE", QVariant( noteLineEdit->text() ) );
    }
    else
    {
    record.setNull("CAT_NOTE");
    }

    if (priceFCheckBox->isChecked() )
    {
    record.setValue( "CAT_PR_F", QVariant( prFDoubleSpinBox->value() ) );
    }
    else
    {
    record.setNull("CAT_PR_F");
    }

    if (priceVFCheckBox->isChecked() )
    {
    record.setValue( "CAT_PR_VF", QVariant( prVFDoubleSpinBox->value() ) );
    }
    else
    {
    record.setNull("CAT_PR_VF");
    }

    if (priceXFCheckBox->isChecked() )
    {
    record.setValue( "CAT_PR_XF", QVariant( prXFDoubleSpinBox->value() ) );
    }
    else
    {
    record.setNull("CAT_PR_XF");
    }

    if (priceUNCCheckBox->isChecked() )
    {
    record.setValue( "CAT_PR_UNC", QVariant( prUNCDoubleSpinBox->value() ) );
    }
    else
    {
    record.setNull("CAT_PR_UNC");
    }

    if (currCheckBox->isChecked() )
    {
    record.setValue( "CAT_CURR", QVariant( currComboBox->currentText() ) );
    }
    else
    {
    record.setNull("CAT_CURR");
    }

return record;
}

void CatRowWidget::new_record( qint64 coin_id, QString opened_album )
{

    if ( true == opened_album.isEmpty() )
    {
    return;
    }

QSqlRecord record = QSqlRecord();

qint64 cat_id = Q_INT64_C(-1);
    {
    QSqlDatabase db = QSqlDatabase::database("collection");
       if ( !db.isOpen() )
       {
       return;
       }
    QString s = QLatin1String("select gen_id(");
    s.append(opened_album);
    s.append(QLatin1String("_catalogs_gen,1) from rdb$database"));
    QSqlQuery query(s, db);
        while (query.next())
        {
        cat_id = query.value(0).toLongLong();
        }
    query.clear();
    }

    if (cat_id == -1)
    {
    return;
    }

is_new = true;

record.insert( 0, QSqlField("CAT_ID", QVariant::LongLong) );
record.setValue("CAT_ID", cat_id);

record.insert( 1, QSqlField("COIN_ID", QVariant::LongLong) );
record.setValue("COIN_ID", coin_id);

record.insert( 2, QSqlField("CAT_NAME", QVariant::String) );
record.insert( 3, QSqlField("INT_ID", QVariant::String) );
record.insert( 4, QSqlField("CAT_NOTE", QVariant::String) );
record.insert( 5, QSqlField("CAT_PR_F", QVariant::Double) );
record.insert( 6, QSqlField("CAT_PR_VF", QVariant::Double) );
record.insert( 7, QSqlField("CAT_PR_XF", QVariant::Double) );
record.insert( 8, QSqlField("CAT_PR_UNC", QVariant::Double) );
record.insert( 9, QSqlField("CAT_CURR", QVariant::String) );
record.insert( 10, QSqlField("RESERVED0", QVariant::String) );

current_record = QSqlRecord(record);
}

void CatRowWidget::save_clicked()
{
QSqlRecord record = read_record(current_record);
emit record_to_commit(record, current_index, QLatin1String("CAT_ID"), is_new);
close();
}

void CatRowWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("CatRowWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.endGroup();
}

void CatRowWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);                
settings.beginGroup( QLatin1String("CatRowWidget") );
resize( settings.value( QLatin1String("size"), QSize( 400, 346 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void CatRowWidget::closeEvent( QCloseEvent * e )
{
writeSettings();
e->accept();
}
